package com.credit.management.system.web.controller;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.credit.management.system.model.Page;
import com.credit.management.system.model.Syrole;
import com.credit.management.system.model.Syroleauthority;
import com.credit.management.system.service.ISyroleService;
import com.credit.management.system.service.ISyroleauthorityService;


@Controller
@RequestMapping(value="/role")
public class SyroleController {
	@Autowired
	private ISyroleService syroleService;
	@Autowired
	private ISyroleauthorityService syroleauthorityService;
	
	/**
	 * 进入list页面
	 * @return
	 */
	@RequestMapping(value="/role",method=RequestMethod.GET)
	public String list(){
	
		return "syauthority/role";	
	}
	@RequestMapping(value="/list",method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> findlist(Integer start, Integer length, Object total)
			throws UnsupportedEncodingException {
		if (start == null) {
			start = 0;
		};
		if (length == null) {
			length = 5;
		};
		// 查总条数（单表查询），先去联系怎删改
		Page page=new Page(start,length);
		page=syroleService.findPageSyrole(page);
		
		
		Map<String,Object> map=new HashMap<String, Object>();
		map.put("aaData", page.getList());//返回的数据
		map.put("iTotalRecords", page.getTotal());//总条数
		map.put("iTotalDisplayRecords",page.getTotal());////总条数
		return map;
	}
	
	/**
	 * 添加角色
	 * @param rolename
	 * @param roledetail
	 * @return
	 */
	@RequestMapping(value = "/add")
	@ResponseBody
	public Integer add(String rolename,String roledetail){
		
		System.out.println(rolename);
		System.out.println(roledetail);
		Syrole syrole=new Syrole();
		syrole.setRolename(rolename);
		syrole.setRoledetail(roledetail);
	
	
		
		int num=syroleService.insertSelective(syrole);
		
	
			
		return num;
	}
	/**
	 * 修改
	 * @param syrole
	 * @return
	 */
	@RequestMapping(value = "/updaterole")
	@ResponseBody
	public Integer updaterole(Syrole syrole) {
		
		Map<String,Object> map =new HashMap<>();
		
		Integer num=syroleService.updateByPrimaryKeySelective(syrole);
		System.out.println(num);
		return num;
	}
	/**
	 * 根据id查询
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/findbyid")
	@ResponseBody
	public Syrole findbyid(int id){
		System.out.println(id);
		
		Map<String,Object> map =new HashMap<>();
		
		Syrole syrole=syroleService.selectByPrimaryKey(id);
		
		return syrole;
	}
	/**
	 * 删除
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/deletebyid")
	@ResponseBody
	public Integer deletebyid(Integer id){
		Map<String,Object> map =new HashMap<>();
		Integer num=syroleService.deleteByPrimaryKey(id);
		return num;
	}
	
	/**
	 * 查询该用户所具有的权限和不具有的权限
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/grant",method=RequestMethod.POST)
	@ResponseBody
	public List<Map<String,Object>> grant(Integer id){
		List<Map<String,Object>> maps=syroleService.findRoleExistPermissionsAndNotExistPermissions(id);
		return maps;
	}
	
	@RequestMapping(value="/grantPermission",method=RequestMethod.POST)
	@ResponseBody
	public Integer grantRole(Integer id,Integer[] authids){
		System.out.println("id:"+id);
		//删除该角色的所有权限
		syroleauthorityService.deleteRolePermissionsByRoleId(id);
		if(authids!=null &&authids.length>0){
			
			for(int i=0;i<authids.length;i++){
				Syroleauthority syroleauthority=new Syroleauthority ();
				syroleauthority.setAuthid(authids[i]);
				syroleauthority.setRoleid(id);
				System.out.println("authids[i]:"+authids[i]);
				syroleauthorityService.insertSelective(syroleauthority);
			}
		}
		return 0;
	}
	
	
}
